package com.kedacom.testlego.view.activity

import android.net.Uri
import android.os.Bundle
import com.google.android.exoplayer2.ExoPlayerFactory
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.ExtractorMediaSource
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import com.kedacom.testlego.R
import com.kedacom.testlego.databinding.ActivityExoVideoPlayBinding
import com.kedacom.testlego.viewmodel.ExoVideoPlayViewModel
import kotlinx.android.synthetic.main.activity_exo_video_play.*


class ExoVideoPlayActivity : BaseActivity<ActivityExoVideoPlayBinding, ExoVideoPlayViewModel>() {

    override fun getContentViewId() = R.layout.activity_exo_video_play


    // exoPlayer nesnesi tanımlanıyor.
    private lateinit var simpleExoPlayer: SimpleExoPlayer

    // exoPlayer'da kullanmak icin DataSource nesnesi tanımı
    private lateinit var mediaDataSourceFactory: DataSource.Factory

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        var videoUrl = intent.getStringExtra("videoUrl")
        if (videoUrl.isNullOrEmpty()) {
            videoUrl = "http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4"
        }
        // yeni bir instance baslatılması
        simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this)

        // DataSource içerisini doldurma
        mediaDataSourceFactory =
            DefaultDataSourceFactory(this, Util.getUserAgent(this, "ExoPlayerDemo"))

        // 在v2.10.0或更高版本，使用ProgressiveMediaSource代替ExtractorMediaSource；
        val mediaSource =
            ExtractorMediaSource.Factory(mediaDataSourceFactory)
                .createMediaSource(Uri.parse(videoUrl))

        // player'ı hazır hale getirme
        simpleExoPlayer.prepare(mediaSource, false, false)

        // play oynatılmaya hazır olduğunda video oynatma islemi
        simpleExoPlayer.playWhenReady = true

        // loyout dosyasındaki id degeri eslestirme
        playerView.player = simpleExoPlayer

        // player ekranına focuslanma ozelligi
        playerView.requestFocus()

    }

}
